package com.mapzone.showmap;


import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnKeyListener;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.os.SystemClock;
import android.provider.Settings;
import android.util.DisplayMetrics;
import android.util.FloatMath;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.SimpleAdapter;
import android.widget.TextView;


public class MapActivity extends Activity {

	private AlertDialog menu_dialog;
	private View menu_view;
	private GridView menu_grid;

	/** 菜单图片 **/
	int[] menu_image_array = { 
			R.drawable.menu_pan,	R.drawable.menu_region, R.drawable.menu_entire,
			R.drawable.menu_select, R.drawable.menu_info, R.drawable.menu_sql,
			R.drawable.menu_locate,R.drawable.menu_auto_locate,R.drawable.menu_gps_point,
			R.drawable.menu_zoom_in, R.drawable.menu_zoom_out , R.drawable.menu_close};
	/** 菜单文字 **/
	String[] menu_name_array = { "平移", "拉框缩放", "全图显示", "选择", "信息查看","SQL查询", "手动定位",
			"自动定位", "GPS采点", "放大", "缩小", "关闭菜单" };


	private final int ITEM_PAN = 0;// 平移
	private final int ITEM_REGION = 1;// 拉框缩放
	private final int ITEM_ENTIRE = 2;//全图
	private final int ITEM_SELECT = 3;// 选择
	private final int ITEM_INFO = 4;// i键查询
	private final int ITEM_SQL = 5;// sql查询
	private final int ITEM_LOCATE = 6;// 手动定位
	private final int ITEM_AUTO_LOCATE = 7;// 自动定位
	private final int ITEM_GPS_POINT = 8;// gps采点
	private final int ITEM_ZOOM_IN = 9;// 缩小
	private final int ITEM_ZOOM_OUT = 10;// 放大
	private final int ITEM_CLOSE = 11;// 关闭菜单

	private TextView msg_text;
//	private Button bt_region_zoom;
//	private Button bt_select;
//	private Button bt_entire;
//	private Button bt_drag;
//	private Button bt_show_info;
//	private Button bt_zoom_in;
//	private Button bt_zoom_out;
//	private Button bt_sql;
//	private Button bt_gps;
//	private Button bt_location;
//	private Button bt_auto;

	private MapView mv;

	private String map_doc_path ;

	//	private float x , y ;
	private AlertDialog.Builder builder ;
	private String[] atts;
	private String[] layers;
	private String[] fields;
	private String[] values;

	int layer_index = 0;
	int field_index = 0;
	int value_index = 0;
	int re = 0;

	//	private float dpix, dpiy;
	private double latitude = 0;//纬度
	private double longtitude = 0;//经度
	private double accuracy = 0;//精度
	private double altitude = 0;//高度
	private double time = 0;//
	private double speed = 0;//
	private double bearing = 0;//

	private boolean b_auto = false;
	//	
	private LocationManager location_manager;
	
	private final int LOCATION_TEST_COUNT = 20;
	private final int TOUCH_MOVE_JUDGE = 5;
	
	private float x_old = 0;
	private float y_old = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 
		requestWindowFeature(Window.FEATURE_NO_TITLE);  
		setContentView( R.layout.map_activity );

		Intent intent = getIntent();
		map_doc_path = intent.getStringExtra( "path" );
		
		  
//		bt_region_zoom = ( Button )findViewById( R.id.bt_region_zoom);
//		bt_region_zoom.setOnClickListener( new MapActivityClickListener( ) );

//		bt_select = ( Button )findViewById( R.id.bt_select);
//		bt_select.setOnClickListener( new MapActivityClickListener( ) );

//		bt_entire = ( Button )findViewById( R.id.bt_entire);
//		bt_entire.setOnClickListener( new MapActivityClickListener( ) );

//		bt_drag = ( Button )findViewById( R.id.bt_drag);
//		bt_drag.setOnClickListener( new MapActivityClickListener( ) );

//		bt_show_info = ( Button )findViewById( R.id.bt_show_info);
//		bt_show_info.setOnClickListener( new MapActivityClickListener( ) );

//		bt_zoom_out = ( Button )findViewById( R.id.bt_zoom_out);
//		bt_zoom_out.setOnClickListener( new MapActivityClickListener( ) );

//		bt_zoom_in = ( Button )findViewById( R.id.bt_zoom_in);
//		bt_zoom_in.setOnClickListener( new MapActivityClickListener( ) );

//		bt_sql = ( Button )findViewById( R.id.bt_sql );
//		bt_sql.setOnClickListener( new MapActivityClickListener() );

//		bt_gps = ( Button )findViewById( R.id.bt_gps );
//		bt_gps.setOnClickListener( new MapActivityClickListener() );		

//		bt_location = ( Button )findViewById( R.id.bt_location );
//		bt_location.setOnClickListener( new MapActivityClickListener() );

//		bt_auto = ( Button )findViewById( R.id.bt_auto );
//		bt_auto.setOnClickListener( new MapActivityClickListener() );

		msg_text = ( TextView )findViewById( R.id.msg_text );
		msg_text.setText( map_doc_path );

		mv = ( MapView )findViewById( R.id.map_view );

		menu_view = View.inflate( this, R.layout.gridview_menu, null );
		// 创建AlertDialog
		menu_dialog = new AlertDialog.Builder(this).create();
		menu_dialog.setView( menu_view );

		menu_dialog.setOnKeyListener(new OnKeyListener() {
			public boolean onKey(DialogInterface dialog, int keyCode,
					KeyEvent event) {
				if (keyCode == KeyEvent.KEYCODE_MENU)// 监听按键
					dialog.dismiss();
				return false;
			}
		});
		menu_dialog.setCanceledOnTouchOutside( true );

		menu_grid = (GridView) menu_view.findViewById(R.id.gridview);
		menu_grid.setAdapter(getMenuAdapter(menu_name_array, menu_image_array));
		menu_grid.setBackgroundColor( getResources().getColor( R.color.mediumaquamarine ) );
		/** 监听menu选项 **/
		menu_grid.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				switch (arg2) {
				case ITEM_PAN:
					menu_dialog.dismiss();
					mv.pan();
					break;
				case ITEM_REGION:
					menu_dialog.dismiss();
					mv.region_zoom();
					break;
				case ITEM_ENTIRE:
					menu_dialog.dismiss();
					mv.entire();
					break;
				case ITEM_SELECT:
					menu_dialog.dismiss();
					mv.select();
					break;
				case ITEM_INFO:
					atts = mv.get_object_att();
					Log.v( "SHOWMAP", "" + atts.length );
					for( int i = 0; i < atts.length; i++ )
					{
						Log.v( "SHOWMAP", atts[i] );
					}
					menu_dialog.dismiss();
					builder	= new AlertDialog.Builder( MapActivity.this );
					showAttBuilder( builder );
					break;
				case ITEM_SQL:
					layers = mv.get_layers();
					Log.v( "SHOWMAP", "" + layers.length );
					//					for( int i = 0; i < layers.length; i++ )
					//					{
					//						Log.v( "SHOWMAP", layers[i] );
					//					}
					menu_dialog.dismiss();
					builder	= new AlertDialog.Builder( MapActivity.this );
					showLayerBuilder( builder );
					break;
				case ITEM_LOCATE:
					Log.v( "SHOWMAP",  "bt_location == v ----------into--------------" );
					if( null != location_manager )
					{
						Log.v( "SHOWMAP",  "bt_location == v null != location_manager" );
						location_manager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, locationlistener );
					}
					Location location = location_manager.getLastKnownLocation( LocationManager.GPS_PROVIDER );
					
					int i_count = 0;
					while( null == location && i_count++ < LOCATION_TEST_COUNT )
					{
						Log.v( "SHOWMAP",  "bt_location == v  into while" );
						location = location_manager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
						SystemClock.sleep( 1000 );
					}
					menu_dialog.dismiss();
					if( LOCATION_TEST_COUNT == i_count )
					{
						new AlertDialog.Builder( MapActivity.this )
						.setTitle( R.string.msg_locate_failed )
						.setCancelable( true )
						.show();
					}
					else
					{
						Log.v( "SHOWMAP",  "bt_location == v get ok" );
						latitude = location.getLatitude();
						longtitude = location.getLongitude();
						accuracy = location.getAccuracy();
						altitude = location.getAltitude();
						time = location.getTime();
						speed = location.getSpeed();
						bearing = location.getBearing();
						Log.v( "SHOWMAP",  "-----------------show location----------------" );
						Log.v( "SHOWMAP",  "bt_location  Latitude = " + latitude );
						Log.v( "SHOWMAP",  "bt_location  Longtitude = " + longtitude );
						Log.v( "SHOWMAP",  "bt_location  Accuracy = " + accuracy );
						Log.v( "SHOWMAP",  "bt_location  Altitude = " + altitude );
						Log.v( "SHOWMAP",  "bt_location  Time = " + time );
						Log.v( "SHOWMAP",  "bt_location  Speed = " + speed );
						Log.v( "SHOWMAP",  "bt_location  Bearing = " + bearing );
						Log.v( "SHOWMAP",  "-----------------show location end----------------" );
						Log.v( "SHOWMAP",  "bt_location  bt_location removeUpdates is ok"  );

						mv.locate( longtitude, latitude );
					}
					b_auto = false;
					location_manager.removeUpdates( locationlistener );
					break;
				case ITEM_AUTO_LOCATE:
					if( b_auto )
					{
						if( null != location_manager )
						{
							location_manager.removeUpdates( locationlistener );
						}
						b_auto = false;
					}
					else
					{
						if( null != location_manager )
						{
							Log.v( "SHOWMAP",  "bt_location == v null != location_manager" );
							location_manager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, locationlistener );
						}
						b_auto = true;
					}
					menu_dialog.dismiss();
					break;
				case ITEM_GPS_POINT:
					//					location_manager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 1000, 0, locationlistener );
					if( null != location_manager )
					{
						location_manager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, locationlistener );
					}
					menu_dialog.dismiss();
					mv.init_gps();
					break;
				case ITEM_ZOOM_IN:
					menu_dialog.dismiss();
					mv.zoom_in();
					break;
				case ITEM_ZOOM_OUT:
					menu_dialog.dismiss();
					mv.zoom_out();
					break;
				case ITEM_CLOSE:
					menu_dialog.dismiss();
					break;
				}
			}
		});

		DisplayMetrics dm = new DisplayMetrics();
		this.getWindowManager().getDefaultDisplay().getMetrics(dm);

		location_manager = ( LocationManager )this.getSystemService( Context.LOCATION_SERVICE );

		mv.init( dm.xdpi,  dm.ydpi );
		mv.init_bitmap( dm.widthPixels, dm.heightPixels );
		mv.init_ndk( dm.widthPixels, dm.heightPixels, dm.xdpi, dm.ydpi );
		mv.open_map_doc( map_doc_path );
		Log.v( "", "MapActivity create ok" );
		super.onCreate(savedInstanceState);
	}

	private SimpleAdapter getMenuAdapter(String[] menuNameArray,
			int[] imageResourceArray) {
		ArrayList<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
		for (int i = 0; i < menuNameArray.length; i++) {
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("itemImage", imageResourceArray[i]);
			map.put("itemText", menuNameArray[i]);
			data.add(map);
		}
		SimpleAdapter simperAdapter = new SimpleAdapter(this, data,
				R.layout.item_menu, new String[] { "itemImage", "itemText" },
				new int[] { R.id.item_image, R.id.item_text });
		return simperAdapter;
	}

	private final LocationListener locationlistener = new LocationListener()
	{
		@Override
		public void onLocationChanged(Location location) {
			latitude = location.getLatitude();
			longtitude = location.getLongitude();
			accuracy = location.getAccuracy();
			altitude = location.getAltitude();
			time = location.getTime();
			speed = location.getSpeed();
			bearing = location.getBearing();
			Log.v( "SHOWMAP",  "-----------------show location----------------" );
			Log.v( "SHOWMAP",  "Latitude = " + latitude );
			Log.v( "SHOWMAP",  "Longtitude = " + longtitude );
			Log.v( "SHOWMAP",  "Accuracy = " + accuracy );
			Log.v( "SHOWMAP",  "Altitude = " + altitude );
			Log.v( "SHOWMAP",  "Time = " + time );
			Log.v( "SHOWMAP",  "Speed = " + speed );
			Log.v( "SHOWMAP",  "Bearing = " + bearing );
			Log.v( "SHOWMAP",  "-----------------show location end----------------" );

			mv.position_change();
			mv.set_location(latitude, longtitude, accuracy, altitude, time, speed, bearing );
			if( b_auto )
			{
				mv.locate( longtitude, latitude );
			}
		}
		@Override
		public void onProviderDisabled(String arg0) {
			// TODO Auto-generated method stub

		}
		@Override
		public void onProviderEnabled(String arg0) {
			// TODO Auto-generated method stub

		}
		@Override
		public void onStatusChanged(String provider, int status, Bundle extras) {
			switch( status )
			{
			case LocationProvider.AVAILABLE:
				Log.v( "SHOWMAP",  "AVAILABLE " );
				break;
			case LocationProvider.OUT_OF_SERVICE:
				Log.v( "SHOWMAP",  "OUT_OF_SERVICE " );
				break;
			case LocationProvider.TEMPORARILY_UNAVAILABLE:
				Log.v( "SHOWMAP",  "TEMPORARILY_UNAVAILABLE " );
				break;
			}
		}
	};

	public boolean gps_is_open()
	{
		return location_manager.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER );
	}

	public boolean open_gps()
	{
		Intent myIntent = new Intent( Settings.ACTION_SECURITY_SETTINGS );
		startActivity(myIntent);
		return true;
	}

	public class MapActivityClickListener implements OnClickListener
	{
		public MapActivityClickListener(  )
		{
			super();
		}
		public void onClick( View v )
		{
//			if( bt_region_zoom == v )
//			{
//				mv.region_zoom();
//			}else if( bt_select == v )
//			{
//				mv.select();
//			}else if( bt_entire == v )
//			{
//				mv.entire();
//			}else if( bt_drag == v )
//			{
//				Log.v( "", "into drag" );
//				mv.pan();
//			}else if( bt_show_info == v )
//			{
//				atts = mv.get_object_att();
//				Log.v( "SHOWMAP", "" + atts.length );
//				for( int i = 0; i < atts.length; i++ )
//				{
//					Log.v( "SHOWMAP", atts[i] );
//				}
//				builder	= new AlertDialog.Builder( MapActivity.this );
//				showAttBuilder( builder );
//			}else if( bt_zoom_in == v )
//			{
//				mv.zoom_in();
//			}else if( bt_zoom_out == v )
//			{
//				mv.zoom_out();
//			}else if( bt_sql == v )
//			{
//				layers = mv.get_layers();
//				Log.v( "SHOWMAP", "" + layers.length );
//				//				for( int i = 0; i < layers.length; i++ )
//				//				{
//				//					Log.v( "SHOWMAP", layers[i] );
//				//				}
//				builder	= new AlertDialog.Builder( MapActivity.this );
//				showLayerBuilder( builder );
//			}else if( bt_gps == v )
//			{
//				//				location_manager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 1000, 0, locationlistener );
//				if( null != location_manager )
//				{
//					location_manager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, locationlistener );
//				}
//				mv.init_gps();
//			}else if( bt_location == v )
//			{
//				Log.v( "SHOWMAP",  "bt_location == v ----------into--------------" );
//				if( null != location_manager )
//				{
//					Log.v( "SHOWMAP",  "bt_location == v null != location_manager" );
//					location_manager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, locationlistener );
//				}
//				Location location = location_manager.getLastKnownLocation( LocationManager.GPS_PROVIDER );
//				while( null == location )
//				{
//					Log.v( "SHOWMAP",  "bt_location == v  into while" );
//					location = location_manager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
//					SystemClock.sleep( 1000 );
//				}
//				Log.v( "SHOWMAP",  "bt_location == v get ok" );
//				latitude = location.getLatitude();
//				longtitude = location.getLongitude();
//				accuracy = location.getAccuracy();
//				altitude = location.getAltitude();
//				time = location.getTime();
//				speed = location.getSpeed();
//				bearing = location.getBearing();
//				Log.v( "SHOWMAP",  "-----------------show location----------------" );
//				Log.v( "SHOWMAP",  "bt_location  Latitude = " + latitude );
//				Log.v( "SHOWMAP",  "bt_location  Longtitude = " + longtitude );
//				Log.v( "SHOWMAP",  "bt_location  Accuracy = " + accuracy );
//				Log.v( "SHOWMAP",  "bt_location  Altitude = " + altitude );
//				Log.v( "SHOWMAP",  "bt_location  Time = " + time );
//				Log.v( "SHOWMAP",  "bt_location  Speed = " + speed );
//				Log.v( "SHOWMAP",  "bt_location  Bearing = " + bearing );
//				Log.v( "SHOWMAP",  "-----------------show location end----------------" );
//				location_manager.removeUpdates( locationlistener );
//				Log.v( "SHOWMAP",  "bt_location  bt_location removeUpdates is ok"  );
//
//				mv.locate( longtitude, latitude );
//				b_auto = false;
//			}else if( bt_auto == v )
//			{
//				if( b_auto )
//				{
//					if( null != location_manager )
//					{
//						location_manager.removeUpdates( locationlistener );
//					}
//					b_auto = false;
//				}
//				else
//				{
//					if( null != location_manager )
//					{
//						Log.v( "SHOWMAP",  "bt_location == v null != location_manager" );
//						location_manager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, locationlistener );
//					}
//					b_auto = true;
//				}
//			}
		}
	}

	private void showAttBuilder( AlertDialog.Builder builder )
	{
		StringBuffer sb = new StringBuffer();
		for( int i = 0; i < atts.length; i++ )
		{
			sb.append( atts[i] );
		}
		String str = sb.toString();
		builder.setTitle(R.string.show_att )
		.setMessage( str )
		.show();
	}

	private void showLayerBuilder( AlertDialog.Builder builder )
	{
		builder.setTitle( R.string.show_layers )
		.setItems( layers, new LayerItemClick() )
		.show();
	}

	public class LayerItemClick implements DialogInterface.OnClickListener
	{
		public void onClick( DialogInterface dialog, int item )
		{
			layer_index = item;
			fields = mv.get_fields( layer_index );

			Log.v( "SHOWMAP", "" + fields.length );
			for( int i = 0; i < fields.length; i++ )
			{
				Log.v( "SHOWMAP", fields[i] );
			}
			builder	= new AlertDialog.Builder( MapActivity.this );
			showFieldBuilder( builder );
		}
	}

	private void showFieldBuilder( AlertDialog.Builder builder )
	{
		builder.setTitle( R.string.show_field )
		.setItems( fields, new FieldItemClick() )
		.show();
	}

	public class FieldItemClick implements DialogInterface.OnClickListener
	{
		public void onClick( DialogInterface dialog, int item )
		{
			field_index = item;
			values = mv.get_values( layer_index, field_index );

			Log.v( "SHOWMAP", "" + values.length );
			for( int i = 0; i < values.length; i++ )
			{
				Log.v( "SHOWMAP", values[i] );
			}
			builder	= new AlertDialog.Builder( MapActivity.this );
			showValueBuilder( builder );
		}
	}

	private void showValueBuilder( AlertDialog.Builder builder )
	{
		builder.setTitle( R.string.show_field )
		.setItems( values, new ValueItemClick() )
		.show();
	}

	public class ValueItemClick implements DialogInterface.OnClickListener
	{
		public void onClick( DialogInterface dialog, int item )
		{
			value_index = item;
			re = mv.query_bound( value_index );
			//			builder	= new AlertDialog.Builder( MapActivity.this );
			//			showMsgBuilder( builder );
		}
	}
	//	private void showMsgBuilder( AlertDialog.Builder builder )
	//	{
	//		String msg;
	//		if( -1 == re )
	//		{
	//			msg = getString( R.string.msg_failed );
	//		}
	//		else if( 0 == re )
	//		{
	//			msg = getString( R.string.msg_success );
	//		}
	//		else
	//		{
	//			msg = getString( R.string.msg_success2 );
	//			msg += re;
	//		}
	//		builder.setTitle( R.string.msg )
	//		.setMessage( msg )
	//		.show();
	//	}
	//	
	private boolean b_double = false;
	private float old_spacing = 0;
	private boolean b_one_down = false;
	private float scale = 0;
	public boolean onTouchEvent( MotionEvent event ) 
	{
		float x = event.getX();
		float y = event.getY();
		switch( event.getAction() )
		{
		case MotionEvent.ACTION_DOWN:
			Log.v( "SHOWMAP", "ACTION_DOWN"  );
			this.x_old = x;
			this.y_old = y;
			b_one_down = true;
			mv.on_touch_down( x, y );
			break;
		case MotionEvent.ACTION_MOVE:
			Log.v( "SHOWMAP", "ACTION_MOVE"  );
			//			mv.layout( ( int )( x - this.x ), ( int )( y - this.y ), ( int )( x + mv.getWidth() - this.x ), ( int )( y + mv.getHeight() - this.y ) );
			if( b_double )
			{
				float new_spacing = calculate_spacing( event );
				scale = new_spacing - old_spacing;
			}
			else
			{
				if( !( Math.abs( (int)( x- x_old ) ) < 5 && Math.abs( (int)( y - y_old ) ) < 5 ) )
				{
					mv.on_touch_move( x, y );
				}
			}
			break;
		case MotionEvent.ACTION_UP:
			Log.v( "SHOWMAP", "ACTION_UP"  );
			//如果双指同时离开不会触发action_pointer_2_up，会直接触发action_up
			if( b_double )
			{
				Log.v( "SHOWMAP", "ACTION_POINTER_2_UP scale = " + scale  );
				int i_scale = ( ( int )scale / 60 ) + 1;
				mv.scale_zoom( i_scale );
				b_double = false;
			}
			else
			{
				b_one_down = false;
				mv.on_touch_up( x, y );
			}
			//			mv.pan( x - this.x, y - this.y );
			break;
		case MotionEvent.ACTION_POINTER_2_DOWN:
					Log.v( "SHOWMAP", "ACTION_POINTER_2_DOWN"  );
					old_spacing = calculate_spacing( event );
					//双指点击时会先触发的是单指落下，这里把这一次事件完成。
					b_one_down = false;
					b_double = true;
					mv.on_touch_up( x_old, y_old );
			break;
		case MotionEvent.ACTION_POINTER_2_UP:
//					Log.v( "SHOWMAP", "ACTION_POINTER_2_UP" );
//					float new_spacing = calculate_spacing( event );
//					float scale = new_spacing - old_spacing;
//					Log.v( "SHOWMAP", "ACTION_POINTER_2_UP scale = " + scale  );
//					mv.scale_zoom( scale );
			break;
		}
		return super.onTouchEvent(event);
	}
	
	private float calculate_spacing(MotionEvent event) {
		float x = event.getX(0) - event.getX(1);
		float y = event.getY(0) - event.getY(1);
		return FloatMath.sqrt(x * x + y * y);
	}

	@Override
	public boolean onMenuOpened(int featureId, Menu menu) {
		if (menu_dialog == null) {
			menu_dialog = new AlertDialog.Builder(this).setView( menu_view ).show();
		} else {
			menu_dialog.show();
		}
		return false;// 返回为true 则显示系统menu
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add("menu");// 必须创建一项
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	protected void onRestart() {
		// TODO Auto-generated method stub
		super.onRestart();
		mv.b_invalidate = true;
		mv.invalidate();
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		mv.b_invalidate = true;
		mv.invalidate();
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
//		mv.on_destroy();
	}

	@Override
	protected void onResume() {
		super.onResume();
		mv.b_invalidate = true;
		mv.invalidate();
	}

	@Override
	protected void onPause() {
		if( null != location_manager )
		{
			location_manager.removeUpdates( locationlistener );
		}
		super.onPause();
	}
}
